package org.example.io.reactor;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class Acceptor implements Runnable{
    Selector selector;
    ServerSocketChannel serverSocketChannel;

    public Acceptor(ServerSocketChannel serverSocketChannel, Selector selector){
        this.serverSocketChannel = serverSocketChannel;
        this.selector = selector;
    }
    @Override
    public void run() {
        try {
            SocketChannel accept = serverSocketChannel.accept();
            System.out.println(accept.getLocalAddress() + "——收到一个客户端连接");
            accept.configureBlocking(false);
            accept.register(selector, SelectionKey.OP_READ, new Handler(accept));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
